﻿using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Microsoft.Windows.UserControls
{
    public class ImageButton : Button
    {
        const String PropertyOrientation = "Orientation";
        const String PropertyImageData = "ImageData";
        const String PropertyContentType = "ContentType";
        const String PropertyCultureUI = "CultureUI";

        #region Dependency Properties

        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(PropertyOrientation, typeof(Orientation), typeof(ImageButton), new UIPropertyMetadata(Orientation.Horizontal));
  
        public static readonly DependencyProperty ImageDataProperty = DependencyProperty.Register(PropertyImageData, typeof(Geometry), typeof(ImageButton), new UIPropertyMetadata(null));
       
        public static readonly DependencyProperty ContentTypeProperty = DependencyProperty.Register(PropertyContentType, typeof(ShowContentType), typeof(ImageButton), new UIPropertyMetadata(ShowContentType.All));
        
        public static readonly DependencyProperty CultureUIProperty = DependencyProperty.Register(PropertyCultureUI, typeof(CultureInfo), typeof(ImageButton), new PropertyMetadata(Thread.CurrentThread.CurrentUICulture, new PropertyChangedCallback(OnCultureUIPropertyChanged)));
    
        #endregion
        
        #region Properties
        
        public Orientation Orientation
        {
            get
            {
                return (Orientation)base.GetValue(OrientationProperty);
            }
            set
            {
                base.SetValue(OrientationProperty, value);
            }
        }

        public Geometry ImageData
        {
            get
            {
                return (Geometry)base.GetValue(ImageDataProperty);
            }
            set
            {
                base.SetValue(ImageDataProperty, value);
            }
        }

        public ShowContentType ContentType
        {
            get
            {
                return (ShowContentType)base.GetValue(ContentTypeProperty);
            }
            set
            {
                base.SetValue(ContentTypeProperty, value);
            }
        }

        public CultureInfo CultureUI
        {
            get
            {
                return (CultureInfo)base.GetValue(CultureUIProperty);
            }
            set
            {
                base.SetValue(CultureUIProperty, value);
            }
        }

        #endregion
        
        #region ctor
        
        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
        }
        
        #endregion

        #region Static Methods
            
        static void OnCultureUIPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var control = (sender as WizardButton);
            if (control == null)
                return;
            control.OnCultureUIChanged(args.NewValue as CultureInfo);
        }

        #endregion
        
        protected virtual void OnCultureUIChanged(CultureInfo cultureInfo)
        {
            cultureInfo = cultureInfo == null ? Thread.CurrentThread.CurrentUICulture : cultureInfo;
            if (Properties.Resources.Culture == null || Properties.Resources.Culture.Name != cultureInfo.Name)
                Properties.Resources.Culture = cultureInfo;
        }

        public enum ShowContentType
        {
            All,
            OnlyContent,
            OnlyImage
        }
    }
}